﻿using System;
using System.Collections.Generic;
using UnityEngine;
using XUpdater;

namespace XUtliPoolLib
{
	// Token: 0x020001C3 RID: 451
	public class LoadAsyncTask
	{
		// Token: 0x06000A34 RID: 2612 RVA: 0x000358A4 File Offset: 0x00033AA4
		public void Clear()
		{
			this.loadState = EAsyncLoadState.EFree;
			this.location = "";
			this.hash = 0U;
			this.asset = null;
			this.loadCbList.Clear();
			this.loadType = null;
			this.assetBundleInfo = null;
			this.cbObj = null;
		}

		// Token: 0x06000A35 RID: 2613 RVA: 0x000358F4 File Offset: 0x00033AF4
		public bool Update()
		{
			bool result;
			switch (this.loadState)
			{
			case EAsyncLoadState.EFree:
				result = false;
				break;
			case EAsyncLoadState.EPreLoading:
			{
				if (XSingleton<XUpdater.XUpdater>.singleton.ABManager != null)
				{
					this.assetBundleInfo = XSingleton<XUpdater.XUpdater>.singleton.ABManager.LoadImm(this.hash, this.location, this.ext, null);
				}
				if (this.assetBundleInfo == null)
				{
					this.asset = Resources.Load(this.location, this.loadType);
					XResourceLoaderMgr.resourceLoadCount++;
				}
				else
				{
					int num = this.location.LastIndexOf("/");
					if (num > 0 && num + 1 < this.location.Length)
					{
						string text = this.location.Substring(num + 1);
					}
					else
					{
						string text = this.location;
					}
					this.asset = this.assetBundleInfo.mainObject;
					XResourceLoaderMgr.abLoadCount++;
				}
				this.loadState = EAsyncLoadState.ELoading;
				result = false;
				break;
			}
			case EAsyncLoadState.ELoading:
				this.LoadComplete();
				this.loadState = EAsyncLoadState.EFree;
				result = true;
				break;
			case EAsyncLoadState.EInstance:
			{
				if (this.asset != null)
				{
					int i = 0;
					int count = this.loadCbList.Count;
					while (i < count)
					{
						LoadInfo loadInfo = this.loadCbList[i];
						if (loadInfo.loadCb != null)
						{
							GameObject gameObject = XCommon.Instantiate<UnityEngine.Object>(this.asset) as GameObject;
							XResourceLoaderMgr.instanceCount++;
							XSingleton<XResourceLoaderMgr>.singleton.AssetsRefRetain(this.hash);
							XSingleton<XResourceLoaderMgr>.singleton.LogReverseID(gameObject, this.hash);
							loadInfo.loadCb(gameObject, this.cbObj);
						}
						i++;
					}
					this.loadCbList.Clear();
				}
				result = true;
				break;
			}
			default:
				result = false;
				break;
			}
			return result;
		}

		// Token: 0x06000A36 RID: 2614 RVA: 0x00035B0C File Offset: 0x00033D0C
		public void CancelLoad(LoadCallBack cb)
		{
			int count = this.loadCbList.Count;
			for (int i = count - 1; i >= 0; i--)
			{
				LoadInfo loadInfo = this.loadCbList[i];
				if (loadInfo.loadCb == cb)
				{
					this.loadCbList.RemoveAt(i);
				}
			}
		}

		// Token: 0x06000A37 RID: 2615 RVA: 0x00035B6C File Offset: 0x00033D6C
		private void ReturnNull()
		{
			int i = 0;
			int count = this.loadCbList.Count;
			while (i < count)
			{
				LoadInfo loadInfo = this.loadCbList[i];
				if (loadInfo.loadCb != null)
				{
					loadInfo.loadCb(null, this.cbObj);
				}
				i++;
			}
			this.loadCbList.Clear();
		}

		// Token: 0x06000A38 RID: 2616 RVA: 0x00035BD8 File Offset: 0x00033DD8
		private void LoadComplete()
		{
			if (this.asset == null)
			{
				XResourceLoaderMgr.LoadErrorLog(this.location);
				this.ReturnNull();
			}
			else
			{
				if (XSingleton<XResourceLoaderMgr>.singleton.useNewMgr)
				{
					XResourceLoaderMgr.UniteObjectInfo uniteObjectInfo;
					bool uoiasync = XSingleton<XResourceLoaderMgr>.singleton.GetUOIAsync(this.location, this.hash, null, this.assetBundleInfo, this.isSharedRes, out uniteObjectInfo);
					if (uniteObjectInfo != null)
					{
						int i = 0;
						int count = this.loadCbList.Count;
						while (i < count)
						{
							LoadInfo loadInfo = this.loadCbList[i];
							if (loadInfo.loadCb != null)
							{
								UnityEngine.Object uoi = XSingleton<XResourceLoaderMgr>.singleton.GetUOI(uniteObjectInfo, this.isSharedRes, loadInfo.usePool);
								loadInfo.loadCb(uoi, this.cbObj);
							}
							i++;
						}
						this.loadCbList.Clear();
					}
				}
				else
				{
					XSingleton<XResourceLoaderMgr>.singleton.AddAssetInPool(this.asset, this.hash, this.assetBundleInfo);
					int j = 0;
					int count2 = this.loadCbList.Count;
					while (j < count2)
					{
						LoadInfo loadInfo2 = this.loadCbList[j];
						if (loadInfo2.loadCb != null)
						{
							if (this.isSharedRes)
							{
								this.GetSharedResourceCb(loadInfo2.loadCb, this.assetBundleInfo);
							}
							else
							{
								UnityEngine.Object obj = null;
								if (loadInfo2.usePool && XSingleton<XResourceLoaderMgr>.singleton.GetInObjectPool(ref obj, this.hash))
								{
									loadInfo2.loadCb(obj, this.cbObj);
								}
								else
								{
									this.CreateFromPrefabCb(loadInfo2.loadCb, this.assetBundleInfo);
								}
							}
						}
						j++;
					}
					this.loadCbList.Clear();
				}
			}
		}

		// Token: 0x06000A39 RID: 2617 RVA: 0x00035DD4 File Offset: 0x00033FD4
		private void GetSharedResourceCb(LoadCallBack loadCb, AssetBundleInfo info = null)
		{
			UnityEngine.Object assetInPool = XSingleton<XResourceLoaderMgr>.singleton.GetAssetInPool(this.hash);
			XSingleton<XResourceLoaderMgr>.singleton.AssetsRefRetain(this.hash);
			loadCb(assetInPool, this.cbObj);
		}

		// Token: 0x06000A3A RID: 2618 RVA: 0x00035E14 File Offset: 0x00034014
		private void CreateFromPrefabCb(LoadCallBack loadCb, AssetBundleInfo info = null)
		{
			UnityEngine.Object assetInPool = XSingleton<XResourceLoaderMgr>.singleton.GetAssetInPool(this.hash);
			GameObject gameObject = XCommon.Instantiate<UnityEngine.Object>(assetInPool) as GameObject;
			XSingleton<XResourceLoaderMgr>.singleton.AssetsRefRetain(this.hash);
			XSingleton<XResourceLoaderMgr>.singleton.LogReverseID(gameObject, this.hash);
			loadCb(gameObject, this.cbObj);
		}

		// Token: 0x040004CC RID: 1228
		public EAsyncLoadState loadState = EAsyncLoadState.EFree;

		// Token: 0x040004CD RID: 1229
		public Type loadType = null;

		// Token: 0x040004CE RID: 1230
		public uint hash = 0U;

		// Token: 0x040004CF RID: 1231
		public string location = "";

		// Token: 0x040004D0 RID: 1232
		public string ext = "";

		// Token: 0x040004D1 RID: 1233
		public bool isSharedRes = true;

		// Token: 0x040004D2 RID: 1234
		public UnityEngine.Object asset = null;

		// Token: 0x040004D3 RID: 1235
		public List<LoadInfo> loadCbList = new List<LoadInfo>();

		// Token: 0x040004D4 RID: 1236
		private AssetBundleInfo assetBundleInfo = null;

		// Token: 0x040004D5 RID: 1237
		public object cbObj = null;
	}
}
